package com.storeroompda.tool;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.storeroompda.bean.CodeFile;
import com.storeroompda.entity.BillCodeInfo;
import com.storeroompda.entity.CodeInfo;
import com.storeroompda.utils.Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;


public class FileControl {
    static String tag="FileControl";

    public static void makeFile(String name) {
        final String rootPath = Environment.getExternalStorageDirectory().getPath() + "/";
        File file = new File(rootPath + name);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    public static void deleteFile(String name) {
        final String rootPath = Environment.getExternalStorageDirectory().getPath() + "/";
        File file = new File(rootPath + name);
        if (file.exists()) {
            file.delete();
        }
    }

    public static String readFile(Context context,String name) {
        String res = "";
        try {
            FileInputStream fis;
            fis = context.openFileInput(name);
            byte[] buffer = new byte[8];
            for (int i = 0; i <buffer.length ; i++) {
                buffer[i]='\0';
            }
            int a =fis.read(buffer);
            res = new String(buffer, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    public static void writeFile(Context context,String name,String content) {
        try {
//            String storageState = Environment.getExternalStorageState();
//            if (storageState.equals(Environment.MEDIA_MOUNTED)) {
//                FileOutputStream outputStream = new FileOutputStream(name);
//                outputStream.write(content.getBytes());
//                outputStream.close();
//            }
            FileOutputStream fos = context.openFileOutput(name, Context.MODE_PRIVATE);
            fos.write(content.getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String readList(List<CodeInfo> list) {
        String res = "";
        try {
            for (int i = 0; i <list.size() ; i++) {
                String line = list.get(i).getType()+","+list.get(i).getCode()+","+list.get(i).getCodesn()+"," +
                        list.get(i).getIid()+","+list.get(i).getName()+","+list.get(i).getDate()+"\n";
                res=line+res;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    public static List<CodeFile> analyzeList(List<CodeInfo> clist,List<BillCodeInfo> blist) {
        if(clist==null || clist.size()==0 || blist==null || blist.size()==0)
        {
            return null;
        }
        List<CodeFile> reslist= new ArrayList<CodeFile>();
        try {
            for (int i = 0; i <blist.size() ; i++) {
                CodeFile codeFile= new CodeFile();
                BillCodeInfo bi = blist.get(i);
                codeFile.setName(bi.getName());
                codeFile.setDate(bi.getDate());
                String context="";
                switch (bi.getType())
                {
                    case Utils.rk:
                        codeFile.setType("rk");
                        break;
                    case Utils.ck:
                        codeFile.setType("ck");
                        break;
                    case Utils.dk:
                        codeFile.setType("dk");
                        break;
                    case Utils.tk:
                        codeFile.setType("tk");
                        break;
                }
                for (int j = 0; j <clist.size() ; j++) {
                    if(bi.getDate().equals(clist.get(j).getDate()) && bi.getIdd().equals(clist.get(j).getIid()) && bi.getType()==clist.get(j).getType())
                    {
                        if(clist.get(j).getCode().length()>0 && clist.get(j).getCodesn().length()>0 )//混扫
                        {
                            context=context+clist.get(j).getCode()+","+clist.get(j).getCodesn()+"\r\n";
                        }
                        else if(clist.get(j).getCode().length()>0 && clist.get(j).getCodesn().length()==0 )//物流
                        {
                            context=context+clist.get(j).getCode()+"\r\n";
                        }
                        else if(clist.get(j).getCode().length()==0 && clist.get(j).getCodesn().length()>0 )//SN
                        {
                            context=context+clist.get(j).getCodesn()+"\r\n";
                        }
                    }
                }
                codeFile.setContext(context);
                codeFile.setLength(context.getBytes().length);
                reslist.add(codeFile);
            }
            return reslist;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
